package com.servlet.service.impl;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.servlet.dao.TradeLogDao;
import com.servlet.service.ITradeLogService;
import com.servlet.vo.bean.Account;
import com.servlet.vo.bean.Carrier;
import com.servlet.vo.bean.TradeLog;

public class TradeLogServiceImpl implements ITradeLogService{

	private boolean flag = false;
	private TradeLogDao tradeLogDao;
	
	public TradeLogServiceImpl() throws SQLException{
		tradeLogDao = new TradeLogDao();
	}
	
	/**
	 * @param id 账户id
	 * @return  查询未删除账户，有则返回账户实体，无则返回null
	 */
	public TradeLog getObjById(Long id) {
		return tradeLogDao.getObj(id);
	}
	
	public boolean save(HttpServletRequest req) throws SQLException, UnsupportedEncodingException{
		
		flag = true;
		/** 交易类型：1_收入 ；0_支出  ；-1_冲账 */
		int tradeType = Integer.parseInt(req.getParameter("trade_type"));
		BigDecimal amount = new BigDecimal(req.getParameter("amount"));
		String correct_trade = req.getParameter("correct_trade");
		
		Carrier carrier = new CarrierServiceImpl().getObjById(Long.parseLong(req.getParameter("carrier")));
		Account acc = new AccountServiceImpl().getObjById(Long.parseLong(req.getParameter("acc")));
		
		if(-1 == amount.compareTo(BigDecimal.ZERO)){
			amount = amount.negate();
		}
		if(tradeType == 0){ // 支出
			amount = amount.negate();// 金额改为  负值
		}else if(tradeType == -1){ // 冲正
			TradeLog tradeLog = new TradeLogServiceImpl().getObjById(Long.parseLong(correct_trade));
			if(tradeLog.getTrade_type() == 1){ // 收入 的入账冲正值应为 负值
				amount = amount.negate();
			}else if(tradeLog.getTrade_type() == -1){ // 冲账的记录 不能再冲
				flag = false;
			}
		}
		if(flag){
			carrier.setAvailableBalance(carrier.getAvailableBalance().add(amount));
			flag = new CarrierServiceImpl().refreshAvailableBalance(carrier);
			if(flag) System.out.println("更新账户["+acc.getAc_name()+"]"+carrier.getCa_name()+"金额,变动金额【"+amount+"】,更新后可用余额【"+carrier.getAvailableBalance()+"】");
		}
		if(flag){
			acc.setAvailableBalance(acc.getAvailableBalance().add(amount));
			flag = new AccountServiceImpl().refreshAvailableBalance(acc);
			if(flag) System.out.println("更新账户["+acc.getAc_name()+"],更新后可用余额【"+acc.getAvailableBalance()+"】");
		}
		if(flag){
			
			TradeLog obj = new TradeLog();
			obj.setCarrier(carrier.getId());
			obj.setAcc(acc.getId());
			obj.setTrade_desp(req.getParameter("trade_desp"));
			obj.setTrade_type(tradeType);
			if(tradeType == -1)
				obj.setCorrect_trade(Long.parseLong(req.getParameter("correct_trade")));
			obj.setAmount(new BigDecimal(req.getParameter("amount")));
			obj.setCarrierAvailableBalance(carrier.getAvailableBalance());
			obj.setAccountAvailableBalance(acc.getAvailableBalance());
			flag = new TradeLogDao().save(obj);
		}
		
		return flag;
	}
	
	public List<TradeLog> getList(HttpServletRequest req) throws SQLException{
		
		String accId = req.getParameter("acc");
		String carrierId = req.getParameter("carr");
		Map<String, String> params = new HashMap<String, String>();
		params.put("a.acc_id", accId);
		if(null != carrierId && !"".equals(carrierId)){
			params.put("a.carrier_id", carrierId);
		}
		return new TradeLogDao().getListByProperties(params);
	}
	
}
